package br.com.ravi.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DateUtils {
	public static class WeekOfYear {
		private List<Integer> daysOfWeek = new ArrayList<Integer>();
		private int currWeek;
		
		public WeekOfYear(int currWeek, List<Integer> daysOfWeek) {
			this.setCurrWeek(currWeek);
			this.daysOfWeek = daysOfWeek;
		}

		public List<Integer> getDaysOfWeek() {
			return daysOfWeek;
		}

		public void setDaysOfWeek(List<Integer> daysOfWeek) {
			this.daysOfWeek = daysOfWeek;
		}

		public int getCurrWeek() {
			return currWeek;
		}

		public void setCurrWeek(int currWeek) {
			this.currWeek = currWeek;
		}
		
		@Override
		public String toString() {
			return String.format("Semana: %d, Dias: %s", currWeek, daysOfWeek);
		}
	};
	
	public static Calendar[] getDaysOfMonth(Integer nuMonth, Integer nuYear) {
		
		Calendar firstDay = getFirstDayOfMonth(nuMonth, nuYear);
		Calendar lastDay  = getLastDayOfMonth(nuMonth, nuYear);
		
		int fDay = firstDay.get(Calendar.DAY_OF_MONTH);
		int lDay = lastDay.get(Calendar.DAY_OF_MONTH);
		
		Calendar[] calendarArray = new Calendar[lDay];
		
		for (int i = fDay-1; i < calendarArray.length-1; i++) {
			Calendar currentDay = (Calendar)firstDay.clone();
			currentDay.add(Calendar.DAY_OF_MONTH, 1);
			calendarArray[i] = currentDay;
		}
		
		return calendarArray;
	}

	public static Calendar getFirstDayOfMonth(Integer nuMonth, Integer nuYear) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, nuMonth);
		c.set(Calendar.YEAR, nuYear);
		c.set(Calendar.DAY_OF_MONTH, 1);
		
		return c;
	}
	
	public static Calendar getLastDayOfMonth(Integer nuMonth, Integer nuYear) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, nuMonth);
		c.set(Calendar.YEAR, nuYear);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		
		return c;
	}
	
	public static void main(String[] args) {
		List<WeekOfYear> weekOfYear = getWeekOfYear(4, 2014);
		for (WeekOfYear week : weekOfYear) {
			System.out.println(week);
		}
	}
	
	public static List<DateUtils.WeekOfYear> getWeekOfYear(int month, int year) {
		Calendar firstDay = getFirstDayOfMonth(4, 2014);
		Calendar lastDay = getLastDayOfMonth(4, 2014);
		
		List<DateUtils.WeekOfYear> weeksOfYear = new ArrayList<DateUtils.WeekOfYear>();
		int beginWeek = firstDay.get(Calendar.WEEK_OF_YEAR);
		int endWeek = lastDay.get(Calendar.WEEK_OF_YEAR);
		
		for(int ix=beginWeek;ix<=endWeek;ix++) {
			DateUtils.WeekOfYear weekOfYear = getCurrentWeek(ix, month, year);
			weeksOfYear.add(weekOfYear);
		}
		
		return weeksOfYear;
	}

	public static WeekOfYear getCurrentWeek(int currWeek, int month, int year) {
		List<Integer> daysOfWeek = new ArrayList<Integer>();
		Calendar currDay = Calendar.getInstance();
		currDay.set(Calendar.YEAR, year);
		currDay.set(Calendar.MONTH, month);
		currDay.set(Calendar.WEEK_OF_YEAR, currWeek);
		currDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		daysOfWeek.add(currDay.get(Calendar.DAY_OF_YEAR));
		
		for(int ix=Calendar.MONDAY; ix<=Calendar.SATURDAY; ix++) {
			Calendar nextDay = (Calendar) currDay.clone();
			nextDay.set(Calendar.DAY_OF_WEEK, ix);
			daysOfWeek.add(nextDay.get(Calendar.DAY_OF_YEAR));	
		}
		
		return new WeekOfYear(currWeek, daysOfWeek);
	}
}
